// 1到100000之间生成1000个的数并去重和排序存入数组中
function getRandomNum() {
    return Math.floor(Math.random() * (100000 - 1) + 1);
}

function getRandomSortedNum() {
    let arr = [];
    for (let i = 0; i < 1000; i++) {
        arr.push(getRandomNum());
    }
    // console.log(arr);
    let uiqueArr = Array.from(new Set(arr));
    // res.sort(function (a, b) { return a - b });
    let res = quickSort(uiqueArr);
    console.log(res);
    return res;
}

function quickSort(arr, left, right) {
    let length = arr.length;
    left = typeof left != 'number' ? 0 : left;
    right = typeof right != 'number' ? length - 1 : right;
    while (left < right) {
        partitionIndex = getPartitionIndex(left, right, arr);
        quickSort(left, partitionIndex - 1, arr);
        quickSort(partitionIndex + 1, right, arr);
    }
    return arr;
}

function getPartitionIndex(left, right, arr) {
    let prev = left;
    let index = left + 1;
    for (let i = index; i <= right; i++) {
        if (arr[i] < arr[prev]) {
            swap(index, i, arr);
            index++;
        }
    }
    swap(index - 1, prev, arr);
    return index - 1;
}

function swap(i, j, arr) {
    var temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
getRandomSortedNum();